//
//  UserDetail.swift
//  icloud
//
//  Created by raymond on 2022/5/21.
//

import SwiftUI

struct UserDetail: View {
  @EnvironmentObject var ds: DataSource
  var user: TSysUser
  
  var dateRange: ClosedRange<Date> {
    let min = Calendar.current.date(byAdding: .year, value: -1, to: user.birthday)!
    let max = Calendar.current.date(byAdding: .year, value: 1, to: user.birthday)!
    return min...max
  }
  
  var userIndex: Int {
    ds.model.result.firstIndex(where: {$0.id==user.id})!
  }
  
  var body: some View {
    List {
      HStack {
        Text("username").bold()
        Divider()
        TextField("username", text: $ds.model.result[userIndex].username)
      }
      HStack {
        Text("realname").bold()
        Divider()
        TextField("realname", text: $ds.model.result[userIndex].realname)
      }
      DatePicker(selection: $ds.model.result[userIndex].birthday, in: dateRange, displayedComponents: [.date]) {
        Text("Birth Day").bold()
      }
    }
  }
}

struct UserDetail_Previews: PreviewProvider {
  static var data = DataSource().model.result
  static var previews: some View {
    UserDetail(user: data[0])
  }
}
